package com.zjm.principle.lsp;

/**
 * 里氏替换原则
 */
public class LiskovSubstitution02 {
    public static void main(String[] args) {
        test(new Rectangle1(5, 4), 20); // 通过测试
        test(new Square1(5), 25);       // 通过测试
    }

    static void test(Shape shape, int expectedArea) {
        if (shape.area() == expectedArea) {
            System.out.println("通过测试");
        } else {
            System.out.println("失败");
        }
    }
}

// 让原来的父类和子类实现或继承一个基接口或基类
interface Shape {
    int area();
}

class Rectangle1 implements Shape {
    private final int width;
    private final int height;

    public Rectangle1(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public int area() {
        return width * height;
    }
}

class Square1 implements Shape {
    private int side;

    // 如果想使用Rectangle1可以使用组合的方式
    private Rectangle1 rectangle1 = new Rectangle1(side,side);

    public Square1(int side) {
        this.side = side;
    }

    public int area() {
        return side * side;
    }
}
